<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='友链管理',active='links'">
<!-- 友链管理页面 - 管理友情链接 -->
<header th:replace="admin/header::headerFragment(${title},${active})"></header>
<body class="fixed-left">
<div id="wrapper">
    <!-- 引入头部导航 -->
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">友链管理</h4>
                    </div>
                    <div class="col-md-12">
                        <!-- 友链列表表格 -->
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>链接名称</th>
                                <th>链接地址</th>
                                <th>链接LOGO</th>
                                <th>链接排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <!-- 遍历友链列表 -->
                            <th:block th:each="link : ${links}">
                                <tr th:attr="mid=${link.mid}">
                                    <!-- 链接名称 -->
                                    <td th:text="${link.name}"></td>
                                    <!-- 链接地址 -->
                                    <td><a th:href="@{${link.slug}}" target="_blank" th:text="${link.slug}"></a></td>
                                    <!-- 链接LOGO/描述 -->
                                    <td th:text="${link.description}"></td>
                                    <!-- 链接排序 -->
                                    <td th:text="${link.sort}"></td>
                                    <!-- 操作按钮 -->
                                    <td>
                                        <!-- 编辑按钮 -->
                                        <a href="javascript:void(0)" onclick="editLink(this);"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-edit"></i> <span>编辑</span></a>
                                        <!-- 删除按钮 -->
                                        <a href="javascript:void(0)" th:onclick="'delLink('+${link.mid}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5"><i
                                                class="fa fa-trash-o"></i> <span>删除</span></a>
                                    </td>
                                </tr>
                            </th:block>
                            </tbody>
                        </table>
                    </div>
                    <!-- 友链编辑表单 -->
                    <div class="row">
                        <div class="col-md-12" style="padding: 20px;">
                            <div class="panel panel-primary">
                                <div class="panel-heading"><h3 class="panel-title">保存友链</h3></div>
                                <div class="panel-body">
                                    <!-- 友链表单 -->
                                    <form id="linkForm" class="form-inline" role="form" novalidate="novalidate">
                                        <!-- 友链ID隐藏字段 -->
                                        <input type="hidden" id="mid" name="mid"/>
                                        <!-- 链接标题输入框 -->
                                        <div class="form-group">
                                            <label class="sr-only">链接标题</label>
                                            <input type="text" id="title" name="title" class="form-control" placeholder="请输入链接标题"
                                                   required="required" aria-required="true"/>
                                        </div>
                                        <!-- 链接URL输入框 -->
                                        <div class="form-group">
                                            <label class="sr-only">链接URL</label>
                                            <input type="url" id="url" name="url" class="form-control" placeholder="请输入链接地址" required="required"
                                                   aria-required="true"/>
                                        </div>
                                        <!-- 链接LOGO输入框 -->
                                        <div class="form-group">
                                            <label class="sr-only">链接LOGO</label>
                                            <input type="url" id="logo" name="logo" class="form-control"
                                                   placeholder="请输入链接LOGO，没有则不输入"/>
                                        </div>
                                        <!-- 链接排序输入框 -->
                                        <div class="form-group">
                                            <label class="sr-only">链接排序</label>
                                            <input type="number" id="sort" name="sort" class="form-control" value="0"/>
                                        </div>
                                        <!-- 保存按钮 -->
                                        <button type="submit" class="btn btn-success waves-effect waves-light m-l-10">保存链接</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 引入页脚内容 -->
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<!-- 引入页脚脚本 -->
<div th:replace="admin/footer :: footer"></div>
<!-- 引入表单验证插件 -->
<script src="//cdn.bootcss.com/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script src="//cdn.bootcss.com/jquery-validate/1.15.1/localization/messages_zh.min.js"></script>
<script type="text/javascript">
    /* 初始化tale工具对象 */
    var tale = new $.tale();

    /* 友链表单验证 */
    $('#linkForm').validate({
        submitHandler: function (form) {
            var params = $("#linkForm").serialize();
            tale.post({
                url : '/admin/links/save',
                data: params,
                success: function (result) {
                    // 清空表单
                    $('#linkForm input').val('');
                    $('#linkForm #sort').val('0');
                    if(result && result.success){
                        tale.alertOkAndReload('友链保存成功');
                    } else {
                        tale.alertError(result.msg || '友链保存失败');
                    }
                }
            });
        }
    });

    /* 编辑友链函数 */
    function editLink(obj) {
        var this_ = $(obj);
        // 获取友链数据
        var mid = this_.parents('tr').attr('mid');
        var title = this_.parents('tr').find('td:eq(0)').text();
        var url = this_.parents('tr').find('td:eq(1)').text();
        var logo = this_.parents('tr').find('td:eq(2)').text();
        var sort = this_.parents('tr').find('td:eq(3)').text();
        // 填充表单
        $('#linkForm #mid').val(mid);
        $('#linkForm #title').val(title);
        $('#linkForm #url').val(url);
        $('#linkForm #logo').val(logo);
        $('#linkForm #sort').val(sort);
    }

    /* 删除友链函数 */
    function delLink(mid) {
        tale.alertConfirm({
            title:'确定删除该链接吗?',
            then: function () {
                tale.post({
                    url : '/admin/links/delete',
                    data: {mid: mid},
                    success: function (result) {
                        if(result && result.success){
                            tale.alertOkAndReload('链接删除成功');
                        } else {
                            tale.alertError(result.msg || '链接删除失败');
                        }
                    }
                });
            }
        });
    }
</script>
</body>
</html>